const path = require("path");
const fs = require("fs");
const expresss = require("express");

const app = expresss();

let keys = fs.readFileSync("./.keys").toString();

app.set("secret", keys);
app.use(expresss.json());
app.use(require("cors")());

app.use("/uploads", expresss.static(path.resolve(__dirname, "uploads")));
app.use("/admin", expresss.static(path.resolve(__dirname, "admin")));
app.use(
  "/",
  (req, res, next) => {
    let deviceAgent = req.headers["user-agent"].toLowerCase();
    let agentID = deviceAgent.match(/(iphone|ipod|ipad|android)/);
    if (agentID || req.url !== "/") {
      next();
    } else {
      res.send("请使用手机访问该页面");
    }
  },
  expresss.static(path.resolve(__dirname, "web"))
);

require("./plugins/db")();
require("./routes/admin")(app);
require("./routes/web")(app);

app.listen(3000, () => {
  console.log("http://localhost:3000");
});
